<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <%-- 页头 --%>
    <title>更新视频 - ReadJava教程</title>
    <%@include file="../common/head.jsp" %>
</head>
<body>
<div class="wrapper">

    <%-- 导航 --%>
    <%@include file="manage-header.jsp" %>

    <%-- 侧边栏 --%>
    <%@include file="manage-sidebar.jsp" %>

    <div class="main-panel">
        <div class="content">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header">
                                <div class="card-title">更新视频</div>
                            </div>
                            <div class="card-body">
                                <div class="row justify-content-md-center">
                                    <div class="col-md-8">
                                        <div class="card">
                                            <div class="card-body">
                                                <form id="updateAcForm">
                                                    <div class="form-group">
                                                        <label>视频编号</label>
                                                        <input type="text" name="videoId" value="${video.videoId}"
                                                               disabled class="form-control input-square videoId">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>视频类型</label>
                                                        <select class="form-control input-square videoTypeId">
                                                            <c:forEach items="${videoCategoryList}" var="vc">
                                                                <option value="${vc.vcId}">${vc.vcTitle}</option>
                                                            </c:forEach>
                                                        </select>
                                                    </div>
                                                    <div class="form-group">
                                                        <label>视频标题</label>
                                                        <input type="text" name="videoTitle"
                                                               value="${video.videoTitle}"
                                                               class="form-control input-square videoTitle">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>视频地址</label>
                                                        <input type="text" name="videoUrl"
                                                               value="${video.videoUrl}"
                                                               class="form-control input-square videoUrl">
                                                    </div>
                                                    <div class="form-group">
                                                        <label>视频图标</label>
                                                        <input type="text" name="videoIcon"
                                                               value="${video.videoIcon}"
                                                               class="form-control input-square videoIcon">
                                                    </div>
                                                    <div class="form-group text-center">
                                                        <label class="form-radio-label">
                                                            <input class="form-radio-input videoState" type="radio"
                                                                   name="videoState" value="0">
                                                            <span class="form-radio-sign">启用视频</span>
                                                        </label>
                                                        <label class="form-radio-label">
                                                            <input class="form-radio-input videoState" type="radio"
                                                                   name="videoState" value="1">
                                                            <span class="form-radio-sign">禁用视频</span>
                                                        </label>
                                                    </div>
                                                </form>
                                            </div>
                                            <div class="card-action text-center">
                                                <button type="button" class="btn btn-info btn-success" id="updateVideoBtn">
                                                    更新视频
                                                </button>
                                                <button type="button" class="btn btn-info btn-danger"
                                                        onclick="window.location.href='/manage/video/category/list'">
                                                    返回管理
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

<%-- js --%>
<%@include file="../common/script.jsp" %>

<script>
    $(function () {
        var videoState = parseInt('${video.videoState}');
        switch (videoState) {
            case 0: $(".videoState").eq(0).attr('checked', true); break;
            case 1: $(".videoState").eq(1).attr('checked', true); break;
            default: alert("设置选中状态bug");
        }

        $(".videoTypeId").val('${video.videoTypeId}');

        $("#updateVideoBtn").on("click", function () {
            var videoId = $(".videoId").val();
            var videoTypeId = $('.videoTypeId option:checked').val();
            var videoTitle = $(".videoTitle").val();
            var videoUrl = $(".videoUrl").val();
            var videoIcon = $(".videoIcon").val();
            var videoState = $('.videoState:checked').val();
            if (videoId != null && videoId.length != 0) {
                if (videoTypeId != null && videoTypeId.length != 0) {
                if (videoTitle != null && videoTitle.length != 0) {
                    if (videoUrl != null && videoUrl.length != 0) {
                        if (videoIcon != null && videoIcon.length != 0) {
                            if (videoState != null && videoState.length != 0) {
                                $.ajax({
                                    type: "POST",
                                    url: "/manage/video/update",
                                    data: {
                                        videoId: videoId,
                                        videoTypeId: videoTypeId,
                                        videoTitle: videoTitle,
                                        videoUrl: videoUrl,
                                        videoIcon: videoIcon,
                                        videoState: videoState
                                    },
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.status == 200) {
                                            alert(data.message);
                                            var video = data.video;
                                            $(".videoId").val(video.videoId);
                                            $(".videoTypeId").val(video.videoTypeId);
                                            $(".videoTitle").val(video.videoTitle);
                                            $(".videoUrl").val(video.videoUrl);
                                            $(".videoIcon").val(video.videoIcon);
                                            switch (video.videoState) {
                                                case 0: $(".videoState").eq(0).attr('checked', true); break;
                                                case 1: $(".videoState").eq(1).attr('checked', true); break;
                                                default: alert("设置选中状态bug");
                                            }
                                        }
                                    },
                                    error: function () {
                                        alert("操作失败：Ajax更新错误");
                                    }
                                });
                            } else {
                                alert("操作失败：未设置视频状态");
                            }
                        } else {
                            alert("操作失败：视频图标不能为空");
                        }
                    } else {
                        alert("操作失败：视频地址不能为空");
                    }
                } else {
                    alert("操作失败：视频标题不能为空");
                }
                } else {
                    alert("操作失败：视频类型不能为空");
                }
            } else {
                alert("操作失败：未设置视频状态");
            }
        });
    });
</script>
</html>